package domain.lexicalAnalyzerGenerator;

import domain.grammar.ExtendedProduction;

public class Function extends Instruction {

	private String name;
	private Instruction code;
	public static Function create(ExtendedProduction extendedProduction) throws InstructionException {
		Function function = new Function();

		function.name = "function" + extendedProduction.get_leftSide().getName();

		if (extendedProduction.get_rightSide() != null){
			function.code = Instruction.create(extendedProduction.get_rightSide().getRootElement());
		}

		return function;
	}

	public String getCall() {
		// TODO Auto-generated method stub
		return this.name + "();";
	}

	public String generateCode() throws CodeGenerationException {
		if (this.code == null){
			throw new CodeGenerationException("El programa trata de usar una funci�n sin c�digo.");
		}
		
		String function = "";// TODO Auto-generated method stub

		function += "private static void " + this.name + "()\n";
		function += "throws ParseFailedException {\n";
		function += code.generateCode();
		function += "}\n";

		return function;
	}
}